package com.example.fragmentbestpracticebyme;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class NewsTitleViewsFragment extends Fragment {


    private  NewsContentFragment newsContentFragment = new NewsContentFragment();

    public  Boolean isPane(){
        return getActivity().findViewById(R.id.layout_right) != null;
    }

    public class NewsTitleViewsAdapter extends RecyclerView.Adapter<NewsTitleViewsFragment.NewsTitleViewsAdapter.ViewHolder> {

        private List<String> titleList;


        public NewsTitleViewsAdapter(List<String> titleList){
            this.titleList = titleList;
            //   this.isPane = isPane;
        }

        @NonNull
        @Override
        public NewsTitleViewsFragment.NewsTitleViewsAdapter.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
            View itemView =   LayoutInflater.from(parent.getContext()).inflate(R.layout.news_title_item,
                    parent,false);

            final NewsTitleViewsFragment.NewsTitleViewsAdapter.ViewHolder viewHolder = new NewsTitleViewsFragment.NewsTitleViewsAdapter.ViewHolder(itemView);
            viewHolder.title = itemView.findViewById(R.id.title_item_textView);
            itemView.findViewById(R.id.title_item_textView)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(isPane()){

                                newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.content);
                                newsContentFragment.reFresh(viewHolder.title.getText().toString(),"Content");

                            }else {
                                Intent intent = new Intent(getContext(),NewsContentActivity.class);
                                intent.putExtra("title",viewHolder.title.getText().toString());
                                intent.putExtra("content",viewHolder.title.getText().toString()+"content");
                                getContext().startActivity(intent);
                            }
                        }
                    });
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull NewsTitleViewsFragment.NewsTitleViewsAdapter.ViewHolder holder, int position) {
            String title = titleList.get(position);
            holder.title.setText(title);
        }

        @Override
        public int getItemCount() {
            return titleList.size();
        }



       class ViewHolder extends RecyclerView.ViewHolder{
            public TextView title;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }

        }
    }


    private View view;
    private List<String> titleList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.news_title_recycle,container,false);
        RecyclerView recyclerView = view.findViewById(R.id.news_title_views);
        createTitleList();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        NewsTitleViewsAdapter adapter = new NewsTitleViewsAdapter(titleList);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


    private void createTitleList(){
        for(int i = 0;i<50;i++){
            titleList.add("News title "+i);
        }
    }
}
